package com.project.web.controller.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.project.user.domain.UserAddress;
import com.project.user.service.IUserAddressService;
import com.project.web.controller.common.BaseController;
import com.project.common.base.AjaxResult;

/**
 * 用户收货地址 信息操作处理
 *
 * @author administrator
 */
@Controller
@RequestMapping("/user/userAddress")
public class UserAddressController extends BaseController {
    private String prefix = "normal";

    @Autowired
    private IUserAddressService userAddressService;

    /**
     * 跳转到新增用户收货地址页面
     *
     * @param modelMap
     * @return
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        modelMap.put("userAddress", new UserAddress());
        return prefix + "/user_addr_save";
    }

    /**
     * 新增/编辑保存用户收货地址
     *
     * @param userAddress
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserAddress userAddress) {
        userAddress.setUserId(this.getUserId());
        if (userAddress.getId().longValue() == 0) {
            return toAjax(userAddressService.insertUserAddress(userAddress));
        } else {
            return toAjax(userAddressService.updateUserAddress(userAddress));
        }
    }

    /**
     * 修改用户收货地址
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("userAddress", userAddressService.selectUserAddressById(id));
        return prefix + "/user_addr_save";
    }

    /**
     * 删除用户收货地址
     *
     * @param id
     * @return
     */
    @RequestMapping("/remove")
    public String remove(String id) {
        userAddressService.deleteUserAddressByIds(id);
        return "redirect:/user/addr";
    }

    /**
     * 修改用户收货地址
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/setDefault/{id}")
    public String setDefault(@PathVariable("id") Long id, ModelMap mmap) {
        userAddressService.resetUserAddressDefault(this.getUserId(), id);
        return "redirect:/user/addr";
    }
}
